package com.mini.grpc.server;

import com.mini.grpc.transport.netty.NettyServerTransport;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
 * 基于Netty的服务端构建器实现
 * 
 * @author Mini-gRPC
 */
public class NettyServerBuilder extends ServerBuilder<NettyServerBuilder> {
    
    private final int port;
    private final List<ServerServiceDefinition> services = new ArrayList<>();
    private final List<ServerInterceptor> interceptors = new ArrayList<>();
    private Executor executor;
    private long handshakeTimeoutMillis = 120000; // 2分钟默认握手超时
    private int maxInboundMessageSize = 4 * 1024 * 1024; // 4MB默认最大消息大小
    private int maxInboundMetadataSize = 8192; // 8KB默认最大元数据大小
    
    /**
     * 构造函数
     * 
     * @param port 监听端口
     */
    public NettyServerBuilder(int port) {
        this.port = port;
    }
    
    @Override
    public NettyServerBuilder addService(ServerServiceDefinition service) {
        if (service == null) {
            throw new IllegalArgumentException("service cannot be null");
        }
        services.add(service);
        return this;
    }
    
    @Override
    public NettyServerBuilder addService(BindableService bindableService) {
        if (bindableService == null) {
            throw new IllegalArgumentException("bindableService cannot be null");
        }
        return addService(bindableService.bindService());
    }
    
    @Override
    public NettyServerBuilder executor(Executor executor) {
        this.executor = executor;
        return this;
    }
    
    @Override
    public NettyServerBuilder intercept(ServerInterceptor interceptor) {
        if (interceptor == null) {
            throw new IllegalArgumentException("interceptor cannot be null");
        }
        interceptors.add(interceptor);
        return this;
    }
    
    @Override
    public NettyServerBuilder handshakeTimeout(long timeout, TimeUnit unit) {
        if (timeout <= 0) {
            throw new IllegalArgumentException("timeout must be positive");
        }
        if (unit == null) {
            throw new IllegalArgumentException("unit cannot be null");
        }
        this.handshakeTimeoutMillis = unit.toMillis(timeout);
        return this;
    }
    
    @Override
    public NettyServerBuilder maxInboundMessageSize(int bytes) {
        if (bytes <= 0) {
            throw new IllegalArgumentException("bytes must be positive");
        }
        this.maxInboundMessageSize = bytes;
        return this;
    }
    
    @Override
    public NettyServerBuilder maxInboundMetadataSize(int bytes) {
        if (bytes <= 0) {
            throw new IllegalArgumentException("bytes must be positive");
        }
        this.maxInboundMetadataSize = bytes;
        return this;
    }
    
    @Override
    public Server build() {
        return new ServerImpl(
            port,
            new ArrayList<>(services),
            new ArrayList<>(interceptors),
            executor,
            handshakeTimeoutMillis,
            maxInboundMessageSize,
            maxInboundMetadataSize
        );
    }
} 